*Table_6

do Define_Globals 

*****************************************************************
* Table 6 - Treatment Effect on the Default Probability 
* Stage 1 - estimate a probit specification on the before period to predict 
*           the funding probability
* Stage 2 - estimate a LPM of the default probability using the first
*           stage funding probability as a regressor. Correct the 
*           errors using Murphy-Topel adjustment 
*****************************************************************
use listing_final, clear

*Stage 1
dprobit funded $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM, vce(cluster st_week) 
matrix V0 = e(V)
predict double zhat 
keep listingkey zhat
sort listingkey
save listingkey_pred_fund_prob_3_risk_levels, replace

use listing_final, clear
sort listingkey
merge listingkey using listingkey_pred_fund_prob_3_risk_levels
keep if _merge ==3 
drop _merge 

gen treat_index = . 
replace treat_index = 1 if treat_1
replace treat_index = 2 if treat_2
replace treat_index = 3 if treat_3

egen treat_index_prime = group(treat_index prime_categ)

gen byte cons = 1

*statistics on late loans in the 5 months before the change 
tabstat default_18 if (inrange(month_id,11,15)), by(treat_index_prime)stat(count mean)

*Stage 2
reg default_18 /// 
	zhat $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM, vce(cluster st_week)

*A joint test fo the null hypothesis that all treatment effects are 0			
test $DIFF3

* Derive the Murphy-Topel adjustment for the second stage standard errors 
* Based on Hardin, James W., "The Robust Variance Estimator for Two-Stage Models," 
* The Stata Journal, 2002, 2 (3), 253-266.
matrix V4 = e(V)
predict double yhat
scalar zz = _b[zhat]

matrix accum C = $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM cons zhat ///
			 $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM cons /// 
			 [iw = (`var' - yhat)*(`var' - yhat)*zhat*(1-zhat)*zz], nocons
			 
matrix accum R = $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM cons zhat ///
			 $DIFF3 $DIFF2 $DIFF1 $COVARIATES $WEEK_DUM cons ///
			 [iw = (`var' - yhat)*(`var'-zhat)], nocons			     

matrix C = C[158..315,1..157] 
matrix R = R[158..315,1..157]  

matrix M = V4 + (V4*(C*V0*C' - R*V0*C' - C*V0*R')*V4) 


matrix sub_mat = M[2..10,2..10] 
matrix diag = vecdiag(sub_mat)

*In order to find the "true" std one has to take the square root of each entry of the vector
*It seems that the increase in the standard error is almost non-existant

*The is the corrected variance matrix
matrix list diag
